package com.jrh.web.mall;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jrh.biz.brand.BrandService;
import com.jrh.biz.mall.MallService;
import com.jrh.dal.LabelDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Brand;
import com.jrh.domain.entity.Label;
import com.jrh.domain.entity.Mall;

/**
 * 类MallController.java的实现描述：卖场
 * 
 * @author mmyjse@gmail.com 2012-7-25 上午1:05:42
 */
@Controller
@RequestMapping("/mall")
public class MallController {

    @Autowired
    private MallService  mallService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private LabelDao     labelDao;

    @RequestMapping(method = RequestMethod.GET, value = "/index")
    public String list(@RequestParam(value = "mallId", required = false) Long mallId,
                       @RequestParam(value = "style", required = false) Long style,
                       @RequestParam(value = "type", required = false) String type,
                       @RequestParam(value = "pageIndex", required = false, defaultValue = "0") Integer pageIndex,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                       Model model) {
        model.addAttribute("mallId", mallId);
        model.addAttribute("style", style);
        model.addAttribute("type", type);
        List<Mall> mallList = mallService.findAll();
        model.addAttribute("malls", mallList);

        List<Label> labels = labelDao.listChildLabel(Label.LABEL_STYLE_KEY);
        model.addAttribute("labels", labels);

        Brand brand = new Brand();
        if (style != null) {
            brand.setStyle(style);
        }
        Page<Brand> brands = brandService.listBrandsByCondition(brand, Page.getStartIndex(pageIndex, pageSize),
                                                                pageSize, mallId);
        model.addAttribute("_data_", brands);
        return "mall/index";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/view/{id}")
    public String view(@PathVariable Long id, Model model) {
        Mall mall = mallService.loadById(id);
        model.addAttribute("mall", mall);
        return "mall/view";
    }

}
